/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LaporanUtangPiutang.java
 *
 * Created on Aug 23, 2010, 10:29:00 PM
 */
package com.ivanbiz.gui;

import com.ivanbiz.dao.HutangDAO;
import com.ivanbiz.dao.PiutangDAO;
import com.ivanbiz.helper.CenterRenderer;
import com.ivanbiz.helper.RightRenderer;
import com.ivanbiz.impl.HutangDAOImpl;
import com.ivanbiz.impl.PiutangDAOImpl;
import com.ivanbiz.model.HutangHistory;
import com.ivanbiz.model.PiutangHistory;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author Aprisma
 */
public class LaporanUtangPiutang extends javax.swing.JInternalFrame {

    HutangDAO hutangDAO = new HutangDAOImpl();
    PiutangDAO piutangDAO = new PiutangDAOImpl();
    List<HutangHistory> listHutangHistory = new ArrayList<HutangHistory>();
    List<PiutangHistory> listPiutangHistory = new ArrayList<PiutangHistory>();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd MMMM yyyy");
    NumberFormat numberFormat = NumberFormat.getInstance(Locale.US);

    /** Creates new form LaporanUtangPiutang */
    public LaporanUtangPiutang() {
        initComponents();
        updateData();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        awal = new org.jdesktop.swingx.JXDatePicker();
        labelDari = new javax.swing.JLabel();
        akhir = new org.jdesktop.swingx.JXDatePicker();
        labelKe = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        buttonProses = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableHutangHistory = new javax.swing.JTable();
        textTotalHutang = new javax.swing.JTextField();
        labelTotalHutang = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tablePiutangHistory = new javax.swing.JTable();
        textTotalPiutang = new javax.swing.JTextField();
        labelTotalPiutang = new javax.swing.JLabel();

        setBorder(new org.jdesktop.swingx.border.DropShadowBorder());
        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("Laporan Utang Piutang");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Tanggal", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        labelDari.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelDari.setText("Dari");

        labelKe.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelKe.setText("Ke");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelDari)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(awal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 334, Short.MAX_VALUE)
                .addComponent(labelKe)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(akhir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(awal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelDari)
                    .addComponent(akhir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelKe))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Tanggal", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        buttonProses.setFont(new java.awt.Font("Tahoma", 1, 11));
        buttonProses.setText("Proses");
        buttonProses.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonProsesActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(buttonProses)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(buttonProses)
                .addContainerGap(14, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Tabel Hutang", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        tableHutangHistory.setAutoCreateRowSorter(true);
        tableHutangHistory.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        tableHutangHistory.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tableHutangHistory);

        textTotalHutang.setFont(new java.awt.Font("Tahoma", 1, 11));
        textTotalHutang.setText("jTextField1");

        labelTotalHutang.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelTotalHutang.setText("Total Sisa Hutang");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                        .addComponent(labelTotalHutang)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textTotalHutang, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 339, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textTotalHutang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelTotalHutang))
                .addContainerGap())
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Tabel Piutang", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        tablePiutangHistory.setAutoCreateRowSorter(true);
        tablePiutangHistory.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        tablePiutangHistory.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(tablePiutangHistory);

        textTotalPiutang.setFont(new java.awt.Font("Tahoma", 1, 11));
        textTotalPiutang.setText("jTextField1");

        labelTotalPiutang.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelTotalPiutang.setText("Total Sisa Piutang");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel4Layout.createSequentialGroup()
                        .addComponent(labelTotalPiutang)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textTotalPiutang, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textTotalPiutang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelTotalPiutang))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-800)/2, (screenSize.height-550)/2, 800, 550);
    }// </editor-fold>//GEN-END:initComponents

    private void buttonProsesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonProsesActionPerformed
        try {
            listHutangHistory = hutangDAO.getData(HutangHistory.class, awal.getDate(), akhir.getDate());
            listPiutangHistory = piutangDAO.getData(PiutangHistory.class, awal.getDate(), akhir.getDate());
            updateData();
        } catch (Exception ex) {
            Logger.getLogger(LaporanUtangPiutang.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_buttonProsesActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.jdesktop.swingx.JXDatePicker akhir;
    private org.jdesktop.swingx.JXDatePicker awal;
    private javax.swing.JButton buttonProses;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel labelDari;
    private javax.swing.JLabel labelKe;
    private javax.swing.JLabel labelTotalHutang;
    private javax.swing.JLabel labelTotalPiutang;
    private javax.swing.JTable tableHutangHistory;
    private javax.swing.JTable tablePiutangHistory;
    private javax.swing.JTextField textTotalHutang;
    private javax.swing.JTextField textTotalPiutang;
    // End of variables declaration//GEN-END:variables

    private void updateData() {
        textTotalHutang.setText("");
        textTotalPiutang.setText("");
        updateTabelHutangHistory();
        updateTablePiutangHistory();
        updateTotalPiutangHistory();
        updateTotalHutangHistory();
    }

    private void updateTabelHutangHistory() {
        String[] judul = {"Tanggal Bayar", "No Nota", "Hutang", "Sudah Bayar", "Sisa Hutang"};
        Object[][] isi = new Object[listHutangHistory.size()][5];
        int x = 0;
        for (HutangHistory hutangHistory : listHutangHistory) {
            isi[x][0] = simpleDateFormat.format(hutangHistory.getCreateDate());
            isi[x][1] = hutangHistory.getHut().getNota().getKode();
            isi[x][2] = "Rp " + numberFormat.format(hutangHistory.getHutang()) + ",00";
            isi[x][3] = "Rp " + numberFormat.format(hutangHistory.getBayar()) + ",00";
            isi[x][4] = "Rp " + numberFormat.format(hutangHistory.getSisa()) + ",00";
            x++;
        }
        tableHutangHistory.setModel(new DefaultTableModel(isi, judul) {

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return false;
            }
        });
        TableColumn column0 = tableHutangHistory.getColumnModel().getColumn(0);
        column0.setCellRenderer(new CenterRenderer());
        TableColumn column1 = tableHutangHistory.getColumnModel().getColumn(1);
        column1.setCellRenderer(new CenterRenderer());
        TableColumn column2 = tableHutangHistory.getColumnModel().getColumn(2);
        column2.setCellRenderer(new RightRenderer());
        TableColumn column3 = tableHutangHistory.getColumnModel().getColumn(3);
        column3.setCellRenderer(new RightRenderer());
        TableColumn column4 = tableHutangHistory.getColumnModel().getColumn(4);
        column4.setCellRenderer(new RightRenderer());
    }

    private void updateTablePiutang() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private void updateTablePiutangHistory() {
        String[] judul = {"Tanggal Bayar", "No Nota", "Piutang", "Sudah Bayar", "Sisa Piutang"};
        Object[][] isi = new Object[listPiutangHistory.size()][5];
        int x = 0;
        for (PiutangHistory piutangHistory : listPiutangHistory) {
            isi[x][0] = simpleDateFormat.format(piutangHistory.getCreateDate());
            isi[x][1] = piutangHistory.getPiut().getPembelian().getKode();
            isi[x][2] = "Rp " + numberFormat.format(piutangHistory.getPiutang()) + ",00";
            isi[x][3] = "Rp " + numberFormat.format(piutangHistory.getBayar()) + ",00";
            isi[x][4] = "Rp " + numberFormat.format(piutangHistory.getSisa()) + ",00";
            x++;
        }
        tablePiutangHistory.setModel(new DefaultTableModel(isi, judul) {

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return false;
            }
        });
        TableColumn column0 = tablePiutangHistory.getColumnModel().getColumn(0);
        column0.setCellRenderer(new CenterRenderer());
        TableColumn column1 = tablePiutangHistory.getColumnModel().getColumn(1);
        column1.setCellRenderer(new CenterRenderer());
        TableColumn column2 = tablePiutangHistory.getColumnModel().getColumn(2);
        column2.setCellRenderer(new RightRenderer());
        TableColumn column3 = tablePiutangHistory.getColumnModel().getColumn(3);
        column3.setCellRenderer(new RightRenderer());
        TableColumn column4 = tablePiutangHistory.getColumnModel().getColumn(4);
        column4.setCellRenderer(new RightRenderer());
    }

    private void updateTotalHutangHistory() {
        double total = 0;
        for (HutangHistory hutangHistory : listHutangHistory) {
            total += hutangHistory.getSisa();
        }
        textTotalHutang.setText("Rp " + numberFormat.format(total) + ",00");
    }

    private void updateTotalPiutangHistory() {
        double total = 0;
        for (PiutangHistory piutangHistory : listPiutangHistory) {
            total += piutangHistory.getSisa();
        }
        textTotalPiutang.setText("Rp " + numberFormat.format(total) + ",00");
    }
}
